package com.sweet.imserver.model;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class User {

  private String id;

  private String avatar;

  private String phone;

  private String nickName;

  private String password;

  private String note; // 备注

  private String city; // 城市

  private JsonArray friendList;

  public User() {
  }

  public static User fromJson(String json) {
    JsonObject jsonObject = new JsonObject(json);
    return new User(jsonObject.getString("id"),
      jsonObject.getString("avatar"),
      jsonObject.getString("phone"),
      jsonObject.getString("nickName"),
      jsonObject.getString("password"),
      jsonObject.getString("note"),
      jsonObject.getString("city"),
      jsonObject.getJsonArray("friendList"));
  }

  public String toJson() {
    return new JsonObject()
      .put("id", id)
      .put("phone", phone)
      .put("password", password)
      .put("avatar", avatar)
      .put("nickName", nickName)
      .put("note", note)
      .put("city", city)
      .put("friendList", friendList)
      .encode();
  }


  public User(String avatar, String phone, String nickName, String password, String note, String city, JsonArray friendList) {
    this.id = UUID.randomUUID().toString().replaceAll("-", "");
    this.avatar = avatar;
    this.phone = phone;
    this.nickName = nickName;
    this.password = password;
    this.note = note;
    this.city = city;
    this.friendList = friendList;
  }

  public User(String id, String avatar, String phone, String nickName, String password, String note, String city, JsonArray friendList) {
    this.id = id;
    this.avatar = avatar;
    this.phone = phone;
    this.nickName = nickName;
    this.password = password;
    this.note = note;
    this.city = city;
    this.friendList = friendList;
  }

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getAvatar() {
    return avatar;
  }

  public void setAvatar(String avatar) {
    this.avatar = avatar;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getNickName() {
    return nickName;
  }

  public void setNickName(String nickName) {
    this.nickName = nickName;
  }

  public String getNote() {
    return note;
  }

  public void setNote(String note) {
    this.note = note;
  }

  public String getCity() {
    return city;
  }

  public void setCity(String city) {
    this.city = city;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    User user = (User) o;
    return Objects.equals(id, user.id);
  }

  @Override
  public int hashCode() {
    return Objects.hash(id);
  }

  @Override
  public String toString() {
    return "User{" +
      "id='" + id + '\'' +
      ", avatar='" + avatar + '\'' +
      ", phone='" + phone + '\'' +
      ", nickName='" + nickName + '\'' +
      ", password='" + password + '\'' +
      ", note='" + note + '\'' +
      ", city='" + city + '\'' +
      '}';
  }
}
